@model ERP.Team.ProductionSystem.ProductionDispatchMain
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改生产派单</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <style>
        .layui-form {
            padding: 20px;
        }
        .form-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #333;
        }
    </style>
</head>
<body>
    @if (!string.IsNullOrEmpty(ViewBag.ErrorMessage))
    {
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-alert layui-alert-danger">
                    @ViewBag.ErrorMessage
                </div>
            </div>
        </div>
    }
    else if (Model != null)
    {
        <div class="form-title">修改生产派单</div>
        <form class="layui-form" action="" id="editDispatchForm">
            <input type="hidden" name="Id" value="@Model.Id" />
            
            <div class="layui-form-item">
                <label class="layui-form-label">生产工单编号</label>
                <div class="layui-input-block">
                    <input type="text" name="ProductionWorkOrderCode" value="@Model.ProductionWorkOrderCode" readonly class="layui-input layui-disabled">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">工单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="WorkOrderName" value="@Model.WorkOrderName" lay-verify="required" placeholder="请输入工单名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">任务名称</label>
                <div class="layui-input-block">
                    <input type="text" name="TaskName" value="@Model.TaskName" lay-verify="required" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">工作站名称</label>
                <div class="layui-input-block">
                    <input type="text" name="WorkGroupName" value="@Model.WorkGroupName" lay-verify="required" placeholder="请输入工作站名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">工序名称</label>
                <div class="layui-input-block">
                    <select name="DocumentStatus" lay-verify="required">
                        <option value="">请选择工序名称</option>
                        @if (Model.DocumentStatus == 1)
                        {
                            <option value="1" selected>印刷</option>
                        }
                        else
                        {
                            <option value="1">印刷</option>
                        }
                        @if (Model.DocumentStatus == 2)
                        {
                            <option value="2" selected>复合</option>
                        }
                        else
                        {
                            <option value="2">复合</option>
                        }
                        @if (Model.DocumentStatus == 3)
                        {
                            <option value="3" selected>分切</option>
                        }
                        else
                        {
                            <option value="3">分切</option>
                        }
                        @if (Model.DocumentStatus == 4)
                        {
                            <option value="4" selected>制袋</option>
                        }
                        else
                        {
                            <option value="4">制袋</option>
                        }
                    </select>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">产品物料名称</label>
                <div class="layui-input-block">
                    <input type="text" name="ProductMaterialName" value="@Model.ProductMaterialName" lay-verify="required" placeholder="请输入产品物料名称" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">开始生产日期</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-date"></i>
                        </div>
                        <input type="text" name="ProductionStartTime" id="startDate" value="@Model.ProductionStartTime.ToString("yyyy-MM-dd HH:mm:ss")" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">完成生产日期</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-date"></i>
                        </div>
                        <input type="text" name="ProductionEndTime" id="endDate" value="@Model.ProductionEndTime.ToString("yyyy-MM-dd HH:mm:ss")" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">派单对象</label>
                <div class="layui-input-block">
                    <input type="text" name="OrderTarget" value="@Model.OrderTarget" lay-verify="required" placeholder="请输入派单对象" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="editForm">保存修改</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="closeWindow()">取消</button>
                </div>
            </div>
        </form>
    }
    else
    {
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-alert layui-alert-warning">
                    未找到要修改的派单记录
                </div>
            </div>
        </div>
    }

    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function(){
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;
            
            // 执行一个laydate实例 - 开始时间
            laydate.render({
                elem: '#startDate',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            
            // 执行一个laydate实例 - 结束时间
            laydate.render({
                elem: '#endDate',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            
            // 监听提交
            form.on('submit(editForm)', function(data){
                // 显示加载层
                var loadIndex = layer.load(2, {
                    shade: [0.3, '#000']
                });
                
                var formData = data.field;
                var id = formData.Id;
                
                // 发送AJAX请求
                $.ajax({
                    url: '/ProductionDispatchMain/UpdateDispatch',
                    type: 'POST',
                    data: {
                        id: id,
                        productionDispatchMainDto: {
                            ProductionWorkOrderCode: formData.ProductionWorkOrderCode,
                            WorkOrderName: formData.WorkOrderName,
                            TaskName: formData.TaskName,
                            WorkGroupName: formData.WorkGroupName,
                            DocumentStatus: parseInt(formData.DocumentStatus),
                            ProductMaterialName: formData.ProductMaterialName,
                            ProductionStartTime: formData.ProductionStartTime,
                            ProductionEndTime: formData.ProductionEndTime,
                            OrderTarget: formData.OrderTarget
                        }
                    },
                    success: function(response) {
                        layer.close(loadIndex);
                        if (response.success) {
                            layer.msg('修改成功！', {
                                icon: 1,
                                time: 1500
                            }, function(){
                                // 关闭当前弹窗
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                // 直接跳转到显示页面，而不是刷新
                                parent.location.href = '/ProductionDispatchMain/GetDispatch';
                            });
                        } else {
                            layer.msg('修改失败：' + response.message, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadIndex);
                        layer.msg('提交失败：' + error, {
                            icon: 2,
                            time: 3000
                        });
                    }
                });
                
                return false; // 阻止表单默认提交
            });
        });
        
        // 关闭窗口函数
        function closeWindow() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    </script>
</body>
</html> 